/*
 * @Author: 吴世扬
 * @Date: 2023-03-13 17:38:26
 * @LastEditors: 吴世扬
 * @LastEditTime: 2023-03-13 17:48:33
 * @FilePath: /grid-center-admin/src/views/common/iframe/helper.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from '@/router/index';

interface SelectMapPointData {
    type: 'selectMapPoint';
    data: { lng: number; lat: number };
}
export const selectMapPoint = () => {
    let resolveFn: (data: SelectMapPointData) => void;
    const p = new Promise<SelectMapPointData>((resolve) => {
        resolveFn = resolve;
    });
    // const aaa = window.open('http://utils.cskj-test.top/#/mapTool');
    const targetRoute = router.resolve({
        path: '/iframe',
        query: {
            url: encodeURI(import.meta.env.VITE_UTILS_WEB + '/mapPoint')
        }
    });
    let referenceWindow = window.open(targetRoute.href);
    const handlePoint = (e: any) => {
        if (e.data.type === 'selectMapPoint') {
            if (e.data.data && e.data.data.lng && e.data.data.lat) {
                resolveFn(e.data);
                // formData.value.longitude = e.data.data.lng;
                // formData.value.latitude = e.data.data.lat;
            }
            console.log('选到了');
            referenceWindow?.close();
            // referenceWindow?.removeEventListener('message', handlePoint);
            // referenceWindow = null;
        }
        if (e.data.type === 'close') {
            console.log('关闭了');
            // referenceWindow?.close();
            referenceWindow?.removeEventListener('message', handlePoint);
            referenceWindow = null;
        }
    };
    referenceWindow?.addEventListener('message', handlePoint);
    return p;
};
